<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <title>用户注册</title>
    <style>
        body {
            background: url('https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547_1280.jpg') no-repeat;
            background-size: 100% 130%;
        }

        #login_box {
            width: 20%;
            height: 400px;
            background-color: #00000060;
            margin: auto;
            margin-top: 10%;
            text-align: center;
            border-radius: 10px;
            padding: 50px 50px;
        }

        h2 {
            color: #ffffff90;
            margin-top: 5%;
        }

        #input-box {
            margin-top: 5%;
        }

        span {
            color: #fff;
        }

        input {
            border: 0;
            width: 60%;
            font-size: 15px;
            color: #fff;
            background: transparent;
            border-bottom: 2px solid #fff;
            padding: 5px 10px;
            outline: none;
            margin-top: 10px;
        }

        button {
            margin-top: 50px;
            width: 60%;
            height: 30px;
            border-radius: 10px;
            border: 0;
            color: #fff;
            text-align: center;
            line-height: 30px;
            font-size: 15px;
            background-image: linear-gradient(to right, #30cfd0, #330867);
        }

        #sign_up {
            margin-top: 45%;
            margin-left: 60%;
        }

        a {
            color: #b94648;
        }
    </style>
</head>

<body>
<div id="login_box">
    <h2>REGISTER</h2>
    <form id="form1" action="##" onsubmit="return false" method="post">
        <div id="input_box">
            <input id="user" type="text" name="username" placeholder="请输入用户名"><br/>
            <span id="msg"></span>
        </div>
        <div class="input_box">
            <input type="password" name="password" placeholder="请输入密码">
        </div>
        <div class="input_box">
            <input type="text" name="phone" placeholder="请输入手机号">
        </div>
        <div class="input_box">
            <input type="text" name="code" placeholder="请输入验证码">
            <img src="/captcha" style="width:180px;margin-top: 2px" id="cap"/><br/>
            <span id="codeMsg"></span>
        </div>
        <br>
    </form>
    <button id="btn1">注册</button>
    <br/>
    <span id="tip"></span>

</div>
</body>
<script>
    $(function () {
        //登入
        $("#btn1").click(function () {
            $.ajax({
                url: '/userLogin',
                type: 'post',
                data: $('#form1').serialize(),
                success: function (ret) {
                    if (ret === -1) {
                        $("#codeMsg").css("color", "red");
                        $("#codeMsg").text("验证码错误!");
                        $("#cap").attr('src', "/captcha?" + new Date().getTime());
                    }
                    if (ret === -2) {
                        $("#codeMsg").css("color", "red");
                        $("#codeMsg").text("请填写验证码!");
                    }
                    if (ret === 1) {
                        $("#codeMsg").text("");
                        $("#msg").text("");
                        $("#tip").css("color", "red");
                        $("#tip").text("注册成功!");
                        $("#cap").attr('src', "/captcha?" + new Date().getTime());
                        $("input").val('');
                    }
                }
            });
        });

        $("#cap").click(function () {
            $(this).attr('src', "/captcha?" + new Date().getTime());
        })

        //校验名字是否重复
        $("#user").mouseout(function () {
            var username = $(this).val();
            $.ajax({
                url: '/checkName',
                type: 'post',
                contentType: 'application/json',
                data: {
                    username: username
                },
                success: function (ret) {
                    if (ret === 1) {
                        $("#codeMsg").text("");
                        $("#msg").text("");
                        $("#tip").text("");
                        $("#msg").css("color", "red");
                        $("#msg").text("用户名可以使用！");
                    }
                    if (ret === 2) {
                        $("#codeMsg").text("");
                        $("#msg").text("");
                        $("#tip").text("");
                        $("#msg").css("color", "red");
                        $("#msg").text("用户名已存在！");
                    }
                    if (ret === 3) {
                        $("#codeMsg").text("");
                        $("#msg").text("");
                        $("#tip").text("");
                        $("#msg").css("color", "red");
                        $("#msg").text("用户名不能为空！");
                    }
                }
            });
        });
    });
</script>
</html>
